package com.springboot.frame.web.validate;

/**
 * @author madman
 * @version 1.0
 * @date 2019/8/1 9:47
 * 约束注解
 * 约束注解和普通的注解一样，一个典型的约束注解的定义应该至少包括如下内容
 * @Target({ })   // 约束注解应用的目标元素类型
 * @Retention() // 约束注解应用的时机
 * @Constraint(validatedBy ={})  // 与约束注解关联的验证器
 * <p>
 * public @interface ConstraintName{
 * <p>
 * String message()default " ";   // 约束注解验证时的输出消息
 * <p>
 * Class<?>[]groups() default { };  // 约束注解在验证时所属的组别
 * <p>
 * Class<?extends Payload>[] payload() default { }; // 约束注解的有效负载
 * <p>
 * }
 * @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
 * @Retention(RUNTIME)
 * @Documented
 * @Constraint(validatedBy = {MobileValidator.class})
 * public @interface Mobile {
 * boolean required() default true;
 * <p>
 * String message() default "手机号码格式错误";
 * <p>
 * Class<?>[] groups() default {};
 * <p>
 * Class<? extends Payload>[] payload() default {};
 * }
 * <p>
 * package miaosha.validator;
 * <p>
 * import javax.validation.ConstraintValidator;
 * import javax.validation.ConstraintValidatorContext;
 * <p>
 * import org.apache.commons.lang3.StringUtils;
 * <p>
 * import miaosha.annotation.Mobile;
 * import miaosha.util.ValidatorUtil;
 * <p>
 * public class MobileValidator implements ConstraintValidator<Mobile, String> {
 * private boolean required = false;
 * <p>
 * <p>
 * 初始化
 * public void initialize(Mobile constraintAnnotation) {
 * required = constraintAnnotation.required();
 * }
 * <p>
 * <p>
 * 校验
 * <p>
 * public boolean isValid(String value, ConstraintValidatorContext context) {
 * if (required) {
 * return ValidatorUtil.isMobile(value);
 * } else {
 * if (StringUtils.isEmpty(value)) {
 * return true;
 * } else {
 * return ValidatorUtil.isMobile(value);
 * }
 * }
 * }
 * <p>
 * }
 */

/**
 @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
 @Retention(RUNTIME)
 @Documented
 @Constraint(validatedBy = {MobileValidator.class})
 public @interface Mobile {
 boolean required() default true;

 String message() default "手机号码格式错误";

 Class<?>[] groups() default {};

 Class<? extends Payload>[] payload() default {};
 }
 */


/**
 package miaosha.validator;

 import javax.validation.ConstraintValidator;
 import javax.validation.ConstraintValidatorContext;

 import org.apache.commons.lang3.StringUtils;

 import miaosha.annotation.Mobile;
 import miaosha.util.ValidatorUtil;

 public class MobileValidator implements ConstraintValidator<Mobile, String> {
 private boolean required = false;


 初始化
 public void initialize(Mobile constraintAnnotation) {
 required = constraintAnnotation.required();
 }


 校验

 public boolean isValid(String value, ConstraintValidatorContext context) {
 if (required) {
 return ValidatorUtil.isMobile(value);
 } else {
 if (StringUtils.isEmpty(value)) {
 return true;
 } else {
 return ValidatorUtil.isMobile(value);
 }
 }
 }

 }
 */
